/**
 * $Id: MessageDigestUtil.java,v 1.5 2012/08/16 11:16:25 jiayu.qiu Exp $
 */
package cn.com.artemis.biz.steinsggs.common.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * MessageDigestUtil
 * 加密算法工具类
 *
 * @version V1.0.0
 * @author <a href="chufeng.xu@downjoy.com">徐楚风</a>
 * @since 2017-02-09 16:56
 */
public class MessageDigestUtil {

    public static String getMessageDigest(byte[] buffer, String key) {
        MessageDigest digest;
        try {
            digest=MessageDigest.getInstance(key);
            digest.update(buffer);
            return bytes2Hex(digest.digest());
        } catch(NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String bytes2Hex(byte[] bts) {
        String des="";
        String tmp=null;
        for(int i=0; i < bts.length; i++) {
            tmp=(Integer.toHexString(bts[i] & 0xFF));
            if(tmp.length() == 1) {
                des+="0";
            }
            des+=tmp;
        }
        return des;
    }

    public static String getMD5(byte[] buffer) {
        return getMessageDigest(buffer, "MD5");
    }

    public static String getMD5(String str) {
        try {
            return getMD5(str.getBytes("ISO-8859-1"));
        } catch(UnsupportedEncodingException e) {
        }
        return null;
    }

    public static String getSHA1(byte[] buffer) {
        return getMessageDigest(buffer, "SHA-1");
    }

    public static String getSHA1(String str) {
        try {
            return getSHA1(str.getBytes("ISO-8859-1"));
        } catch(UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getSHA256(byte[] buffer) {
        return getMessageDigest(buffer, "SHA-256");
    }

    public static String getSHA256(String str) {
        try {
            return getSHA256(str.getBytes("ISO-8859-1"));
        } catch(UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * getRandomString
     * 生成随机字符串
     *
     * @param length 表示生成字符串的长度
     * @return java.lang.String
     *
     * @version V1.0.0
     * @author <a href="chufeng.xu@downjoy.com">徐楚风</a>
     * @since 2017-02-20 10:13
     */
    public static String getRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
//    
//    private static List<String> list=new ArrayList<String>();
//    private static int count=0;
//    static{
//         list.add("admin@downjoy.com");           
//         count=list.size();
//    }
//    public static void main(String[] args) {
//        List<String> results=new ArrayList<String>();
//        for(int i=0;i<count;i++){
//            String pwd="";
//            while(true){
//                pwd=getpasswd();
//                if(PasswdRule.isComplex(pwd)){
//                    break;
//                }
//            }
//            String email=list.get(i);
//            results.add(String.format("%s       %s", email,pwd));
//            System.out.println(String.format("update USER set PASSWORD='%s' where email='%s'; ", getSHA256(pwd).toUpperCase(), email));
//            
//        }
//        for(int i=0,n=results.size();i<n;i++){
//            System.out.println(results.get(i));
//        }
//    }
//    
//    private static String getpasswd(){
//        String[] UP={"!","@","#","$","%","^","&","*","(",")"};
//        String[] letter={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
//        String[] di={"1","2","3","4","5","6","7","8","9","0"};
//        Random rand = new Random();
//        int length = rand.nextInt(6)+9;
//        String[] pwd=new String[length];
//        String passwd="";
//        for(int j=0;j<length;j++){
//            int i=rand.nextInt(10000);
//            char a=UUID.randomUUID().toString().charAt(i%36);
//            if(a%7==0){
//                pwd[j]=UP[rand.nextInt(9)];
//            }else if(a%3==0){
//                pwd[j]=di[rand.nextInt(9)];
//            }else  if(a%2==0){
//                pwd[j]=letter[rand.nextInt(25)].toUpperCase();
//            }else{
//                pwd[j]=letter[rand.nextInt(25)];
//            }
//            passwd+=pwd[j];
//        }
//        return passwd;
//    }
    
}
